serverArr=("localhost" "118.89.222.14" "192.168.1.125" "192.168.0.184" "116.62.246.220")
descriptionArr=("LOCAL" "BJ" "LDF"  "DEV" "PROD")
dockerContainerArr=("cecf8cefb795" "2679aa89170a" "neo4j" "95f744fedec7" "c368940f0d32")

function upload()
{
    echo -e "Upload files to server ..."
    mkdir -p ./Olivia/rdf_csv_data
    scp rela.csv yjs@${serverArr[(mode-1)]}:/home/yjs/Olivia/rdf_csv_data
    scp node_ph.csv yjs@${serverArr[(mode-1)]}:/home/yjs/Olivia/rdf_csv_data
    scp node.csv yjs@${serverArr[(mode-1)]}:/home/yjs/Olivia/rdf_csv_data
    echo ""
}

function showModes()
{
    for((i=0;i<${#serverArr[@]-1};i++));
    do
    let j=$i+1
    echo "$j. ${descriptionArr[i]} - ${serverArr[i]}"
    done
    echo ""
}

function getMode()
{
    read -t 20 -p "Please choose upload mode: " mode
    echo ""
    echo -e " √ ${descriptionArr[(mode-1)]} -  ${serverArr[(mode-1)]}\n"    
}

function doImport()
{
    echo -e "Copy file into docker ..."
    docker exec ${dockerContainerArr[(mode-1)]} mkdir -p /rdf_csv_data
    docker cp /Users/velen/Desktop/BlockChain/ONT/doc/Olivia/rela.csv ${dockerContainerArr[(mode-1)]}:/rdf_csv_data
    docker cp /Users/velen/Desktop/BlockChain/ONT/doc/Olivia/node_ph.csv ${dockerContainerArr[(mode-1)]}:/rdf_csv_data
    docker cp /Users/velen/Desktop/BlockChain/ONT/doc/Olivia/node.csv ${dockerContainerArr[(mode-1)]}:/rdf_csv_data
    echo ""

    echo -e "Deleting graph.db ..."
    docker exec ${dockerContainerArr[(mode-1)]} rm -rf /var/lib/neo4j/data/databases/graph.db
    echo ""

    echo -e "Import data ..."
    docker exec ${dockerContainerArr[(mode-1)]} /var/lib/neo4j/bin/neo4j-admin import  --mode csv --nodes /rdf_csv_data/node.csv --nodes /rdf_csv_data/node_ph.csv --relationships /rdf_csv_data/rela.csv --max-memory 1g
    echo ""

    echo -e "Restart Neo4j ..."
    docker restart neo4j
    echo -e "Finished."
}


showModes
getMode

if [ ${descriptionArr[(mode-1)]} = "LOCAL" ]; 
then
    doImport
else
    upload
    
    ssh yjs@${serverArr[(mode-1)]} << EOF

    # ls /home/yjs/Olivia/rdf_csv_data

    echo -e "Copy file into docker ..."
    docker exec ${dockerContainerArr[(mode-1)]} mkdir -p /rdf_csv_data
    docker cp /home/yjs/Olivia/rdf_csv_data/rela.csv ${dockerContainerArr[(mode-1)]}:/rdf_csv_data
    docker cp /home/yjs/Olivia/rdf_csv_data/node_ph.csv ${dockerContainerArr[(mode-1)]}:/rdf_csv_data
    docker cp /home/yjs/Olivia/rdf_csv_data/node.csv ${dockerContainerArr[(mode-1)]}:/rdf_csv_data
    echo ""

    echo -e "Deleting graph.db ..."
    docker exec ${dockerContainerArr[(mode-1)]} rm -rf /var/lib/neo4j/data/databases/graph.db
    echo ""

    echo -e "Import data ..."
    docker exec ${dockerContainerArr[(mode-1)]} /var/lib/neo4j/bin/neo4j-admin import  --mode csv --nodes /rdf_csv_data/node.csv --nodes /rdf_csv_data/node_ph.csv --relationships /rdf_csv_data/rela.csv --max-memory 1g
    echo ""

    echo -e "Restart Neo4j ..."
    docker restart neo4j
    echo -e "Finished."

EOF
fi
